package com.sinog.biz.service.api.impl;

import com.sinog.biz.BizApplication;
import com.sinog.biz.mapper.havexml.other.LogInsertMapper;
import com.sinog.biz.service.api.LogInsertService;
import com.sinog.core.domain.InterfaceLog;
import com.sinog.core.domain.JieKouLog;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @desc 接口对接 - 日志表插入数据实现类
 * @author - ypc
 * @date - 2022/7/25
 */
@Slf4j
@Service
public class LogInsertServiceImpl implements LogInsertService {

    @Resource
    private LogInsertMapper logInsertMapper;
    @Autowired
    private JdbcTemplate bujiJdbcTemplate ;

    /**
     * 省级日志表插入数据
     * @param interfaceLog 日志表对应的实体类
     */
    @Override
    public void insertLog(InterfaceLog interfaceLog){
        logInsertMapper.insert(interfaceLog);
    }

    /**
     * 部级日志表插入数据
     * @param jieKouLog 部级日志表对应的实体类
     */
    @Override
    public void insertbuLog(JieKouLog jieKouLog) {
        String account = jieKouLog.getAccount();
        String time = jieKouLog.getTime();
        String code = jieKouLog.getCode();
        String data = jieKouLog.getData();
        String flag = jieKouLog.getFlag();
        //根据账号查询部门id = 行政区划的code_id --可能有X结尾的
        String departId = logInsertMapper.findDepartIdByAccount(account);
        String departidLast = departId.substring(departId.length()-1);
        //判断是否是社矫接口进入
        if (Constants.JieKouLog.JIEKOULOGX.equals(departidLast)) {
            departId = departId.substring(0,departId.length()-1);
            jieKouLog.setOrigin(Constants.JieKouLog.JIEKOULOG_SJLR);
        } else {
            //监狱接口/看守所接口录入
            String level = logInsertMapper.findLevelByDepartId(departId);
            if (Constants.JieKouLog.JIEKOULOG_JYJGBS.equals(level)) {
                //监狱接口录入
                jieKouLog.setOrigin(Constants.JieKouLog.JIEKOULOG_JYLR);
            }else if (Constants.JieKouLog.JIEKOULOG_KSSJGBS.equals(level)){
                //看守所接口录入
                jieKouLog.setOrigin(Constants.JieKouLog.JIEKOULOG_KSSLR);
            }
        }
        //引入司法部数据源
        ApplicationContext applicationContext = BizApplication.applicationContext;
        bujiJdbcTemplate = (JdbcTemplate)applicationContext.getBean("bujiJdbcTemplate");
        String sql = "insert into sjiekou_log(account,scode_id,time,code,data,flag,origin)" +
                "values('" + account + "'," + departId + ",'" + time + "','" + code + "','" + data + "'," + flag + "," + jieKouLog.getOrigin() + ")";
        log.info(sql);
        bujiJdbcTemplate.update(sql);
    }
}